﻿using System;
namespace YanZhiwei.DotNet2.Utilities.Common
{
    /// <summary>
    ///Array 帮助类
    /// </summary>
    public static class ArrayHelper
    {
        #region 数组复制（截取）
        /// <summary>
        /// 数组复制（截取）
        /// <para>eg: ArrayHelper.Copy(new int[5] { 1, 2, 3, 4, 5 }, 0, 3);==>new int[3] { 1, 2, 3 }</para>
        /// </summary>
        /// <typeparam name="T">数组类型</typeparam>
        /// <param name="source">需要截取的数组</param>
        /// <param name="startIndex">开始截取位置，从零开始</param>
        /// <param name="endIndex">截取位置</param>
        /// <returns>截取的新数组</returns>
        public static T[] Copy<T>(T[] source, int startIndex, int endIndex)
        {
            if (startIndex < endIndex)
            {
                int _len = endIndex - startIndex;
                T[] _destination = new T[_len];
                Array.Copy(source, startIndex, _destination, 0, _len);
                return _destination;
            }
            return source;
        }
        #endregion
        #region 比较两个数组内容是否一样
        /// <summary>
        /// 比较两个数组内容是否一样
        /// <para>eg:ArrayHelper.Equals(new int[5] { 1, 2, 3, 4, 5 }, new int[5] { 1, 2, 3, 4, 5 });==>true</para>
        /// </summary>
        /// <param name="source">源数组</param>
        /// <param name="compare">比较的数组</param>
        /// <returns>相等返回TRUE;不相等返回FALSE</returns>
        public static bool Equals<T>(this T[] source, T[] compare)
        {
            if (source == null || compare == null)
                return false;
            if (source.Length != compare.Length)
                return false;
            for (int i = 0; i < source.Length; i++)
            {
                if (!source[i].Equals(compare[i]))
                {
                    return false;
                }
            }
            return true;
        }
        #endregion
        #region 按位插入数组
        /// <summary>
        /// 按位插入数组
        /// <para>eg: ArrayHelper.Insert(new int[5] { 1, 2, 3, 4, 5 }, 3, 0);==>new int[6] { 3, 2, 3, 4, 5,1 }</para>
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="source">需要插入的数组</param>
        /// <param name="t">需要插入的数值</param>
        /// <param name="index">需要插入的索引，从0开始</param>
        /// <returns>更新后的数组</returns>
        public static T[] Insert<T>(this T[] source, T t, int index)
        {
            int _length = source.Length;
            T _temp = source[index];
            Array.Resize<T>(ref source, _length + 1);
            source[index] = t;
            source[_length] = _temp;
            return source;
        }
        #endregion
        #region 判断Array是否NULL或者空
        /// <summary>
        /// 判断Array是否NULL或者空
        /// <para>eg:ArrayHelper.IsNullOrEmpty(new int[0]);==>true</para>
        /// </summary>
        /// <param name="source">Array</param>
        /// <returns>是否NULL或者空</returns>
        public static bool IsNullOrEmpty(this Array source)
        {
            if (source == null || source.Length == 0)
                return true;
            return false;
        }
        #endregion
        #region Array添加
        /// <summary>
        /// Array添加
        /// <para>eg:ArrayHelper.Add(new int[5] { 1, 2, 3, 4, 5 },6);==>new int[6] { 1, 2, 3, 4, 5, 6 }</para>
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="array">Array</param>
        /// <param name="item">需要添加项</param>
        /// <returns>返回新的Array</returns>
        public static T[] Add<T>(this T[] array, T item)
        {
            int _count = array.Length;
            Array.Resize<T>(ref array, _count + 1);
            array[_count] = item;
            return array;
        }
        #endregion
        #region Array添加
        /// <summary>
        ///  Array添加
        ///  <para>eg:ArrayHelper.AddRange(new int[5] { 1, 2, 3, 4, 5 }, new int[2] { 6, 7 });==>new int[7] { 1, 2, 3, 4, 5, 6, 7 }</para>
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="sourceArray">Array</param>
        /// <param name="addArray">Array</param>
        /// <returns>返回新的Array</returns>
        public static T[] AddRange<T>(this T[] sourceArray, T[] addArray)
        {
            int _count = sourceArray.Length;
            int _addCount = addArray.Length;
            Array.Resize<T>(ref sourceArray, _count + _addCount);
            //foreach (T t in addArray)
            //{
            //    sourceArray[_count] = t;
            //    _count++;
            //}
            addArray.CopyTo(sourceArray, _count);
            return sourceArray;
        }
        #endregion
    }
}